package com.huanming.dsjc.modular.university.controller;

import com.huanming.dsjc.common.controller.BaseController;
import com.huanming.dsjc.common.exception.BizExceptionEnum;
import com.huanming.dsjc.common.exception.BussinessException;
import com.huanming.dsjc.common.persistence.model.User;
import com.huanming.dsjc.core.shiro.ShiroKit;
import com.huanming.dsjc.modular.goods.entity.Group;
import com.huanming.dsjc.modular.goods.warpper.GroupWarpper;
import com.huanming.dsjc.modular.university.dao.ClassMapper;
import com.huanming.dsjc.modular.university.dao.GroupMapper;
import com.huanming.dsjc.modular.university.entity.Classes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 分组管理控制器
 *
 * @author xufei
 * @Date 2018-01-29 16:11:34
 */
@Controller
@RequestMapping("/group")
public class GroupController extends BaseController {

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private ClassMapper classMapper;

    private String PREFIX = "/university/group/";

    /**
     * 跳转到分组管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "group.html";
    }

    /**
     * 跳转到添加分组管理
     */
    @RequestMapping("/group_add")
    public String groupAdd(Model model) {
        List<Classes> list = this.classMapper.listClasses("");
        model.addAttribute("classes",list);
        return PREFIX + "group_add.html";
    }

    /**
     * 跳转到修改分组管理
     */
    @RequestMapping("/group_update/{groupId}")
    public String groupUpdate(@PathVariable Integer groupId, Model model) {
        Group group = this.groupMapper.selectById(groupId);
        List<Classes> list = this.classMapper.listClasses("");
        model.addAttribute("classes",list);
        model.addAttribute("groups",group);
        return PREFIX + "group_edit.html";
    }

    /**
     * 跳转到修改分组管理
     */
    @RequestMapping("/group_changepw/{groupId}")
    public String groupChangePW(@PathVariable Integer groupId, Model model) {
        Group group = this.groupMapper.selectById(groupId);
        model.addAttribute("group",group);
        return PREFIX + "group_chpwd.html";
    }
    /**
     * 修改当前用户的密码
     */
    @RequestMapping("/changePwd")
    @ResponseBody
    public Object changePwd(@RequestParam Integer groupId,@RequestParam String groupLoginPassword) {
        Group group = groupMapper.selectById(groupId);
        group.setGroupLoginPassword(groupLoginPassword);
        this.groupMapper.updateById(group);
        return SUCCESS_TIP;
    }

    /**
     * 获取分组管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Group> list = this.groupMapper.list(condition);
        for(Group group:list){
            Classes classes= this.classMapper.selectById(group.getClassId());
            if(classes!=null){
                group.setClassName(classes.getClassName());
            }
        }
        return list;
    }

    /**
     * 新增分组管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Group group) {
        this.groupMapper.insert(group);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除分组管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer groupId) {
        this.groupMapper.deleteById(groupId);
        return SUCCESS_TIP;
    }


    /**
     * 修改分组管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Group group) {
        this.groupMapper.updateById(group);
        return super.SUCCESS_TIP;
    }

    /**
     * 分组管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
